#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2023/8/11 17:24
# @Author  : afraidbenq
# @File    : transfer_data.py
# @Software: PyCharm
# @Github  : github/afraidbenq
# @Email   : 329189987@qq.com
# @Desc    :
import re
import paramiko
from database.mysql_control import MysqlDB

# config_list = [{"db": "mall", "table": "tb_newbee_mall_goods_info", "field": "goods_cover_img",
#                 "pattern": r"/([\w-]+\.(?:png|jpg))"},
#                {"db": "mall", "table": "tb_newbee_mall_goods_info", "field": "goods_detail_content",
#                 "pattern": r'src="[^"]+/([^"/]+\.(?:png|jpg))"'},
# config_list = [{"db": "mall", "table": "tb_oiltour_info_library", "field": "file_html_url",
#                 "pattern": r'/([\w-]+\.html)'},
#                {"db": "mall", "table": "tb_oiltour_info_library", "field": "file_pdf_url",
#                 "pattern": r'/([\w-]+\.pdf)'},
#                {"db": "mall", "table": "tb_oiltour_info_library", "field": "cover_drawing_url",
#                 "pattern": r"/([\w-]+\.(?:png|jpg))"},
# config_list = [{"db": "mall", "table": "tb_oiltour_info_cms_content", "field": "content_details",
#                 "pattern": r'src="[^"]+/([^"/]+\.(?:png|jpg))"'},
#                {"db": "mall", "table": "tb_oiltour_info_cms_content", "field": "content_img",
#                 "pattern": r"/([\w-]+\.(?:png|jpg))"},
# config_list = [{"db": "mall", "table": "tb_newbee_mall_goods_category", "field": "image",
#                 "pattern": r"/([\w-]+\.(?:png|jpg))"},
config_list = [{"db": "mall", "table": "tb_newbee_mall_carousel", "field": "carousel_url",
                "pattern": r"/([\w-]+\.(?:png|jpg|jpeg))"},
               {"db": "mall", "table": "tb_newbee_mall_goods_contract", "field": "contract",
                "pattern": r'/([\w-]+\.html)'},
               ]


def ceshi():
    a = MysqlDB()
    b = MysqlDB(local=True)
    for config_dict in config_list:
        db = config_dict['db']
        table = config_dict['table']
        field = config_dict['field']
        pattern = config_dict['pattern']
        q_sql = f"SELECT {field} FROM `{db}`.`{table}`"
        data = a.query(q_sql)
        # print(data)
        print(len(data))
        for i in data:
            matches = re.findall(pattern, i[field])
            if matches:
                filenames = matches
                print(filenames)
            break
            # for j in filenames:
            #     try:
            #         in_sql = f"INSERT INTO `enterprise`.`file`(`file_name`) VALUES ('{j}');"
            #         b.execute(sql=in_sql)
            #     except:
            #         pass


def main():
    a = MysqlDB()
    b = MysqlDB(local=True)
    for config_dict in config_list:
        db = config_dict['db']
        table = config_dict['table']
        field = config_dict['field']
        pattern = config_dict['pattern']
        q_sql = f"SELECT {field} FROM `{db}`.`{table}`"
        data = a.query(q_sql)
        print(len(data))
        for i in data:
            if not i[field]:
                continue
            matches = re.findall(pattern, i[field])
            if matches:
                filenames = matches
                for j in filenames:
                    try:
                        in_sql = f"INSERT INTO `enterprise`.`file`(`file_name`) VALUES ('{j}');"
                        b.execute(sql=in_sql)
                    except:
                        pass


def ssh():
    # 创建 SSHClient 对象
    client = paramiko.SSHClient()

    # 允许自动接受 SSH 主机密钥
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())

    # 连接远程主机
    hostname = '120.76.44.23'
    username = 'ben'
    password = '@kV%5RJniv'
    client.connect(hostname, username=username, password=password)
    return client


def cp():
    # 拿数据
    b = MysqlDB(local=True)
    q_sql = f"SELECT file_name FROM `enterprise`.`file` ORDER BY `id` DESC LIMIT 0,20"
    data = b.query(q_sql)
    print(len(data))
    # ssh主机
    client = ssh()
    for i in data:
        print(i['file_name'])
        file_name = i['file_name']
        # 执行cp
        command = f'cp /data/static/mall/{file_name} /data/static/tmp/{file_name}'
        stdin, stdout, stderr = client.exec_command(command)
        output = stdout.read().decode().strip()
        # 打印命令输出
        print(output)
    # 关闭 SSH 连接
    client.close()

if __name__ == '__main__':
    # main()
    # ceshi()
    cp()
